package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;


    @GetMapping("/page")
    public R PageById(int page,int pageSize,String name){
        Page<SetmealDto> byIdWhitDish = setmealService.getByIdWhitDish(page, pageSize, name);
        return  R.success(byIdWhitDish)    ;
    }

    /**
     * 新增菜品
     */
    //转换为json方式
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R save(@RequestBody SetmealDto dto ){
        setmealService.saveWhitDish(dto);
        return R.success("添加成功");
    }


    /**
     * 批量删除
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        log.info("删除的套餐的id：{}",ids);
        setmealService.removeWhitDish(ids);
        return  R.success("删除成功");
    }

    /**
     * 修改套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        log.info("接收到前端参数：{}", id);
        SetmealDto setmealDto = setmealService.updateWhitDish(id);
        return  R.success(setmealDto);
    }

    @PutMapping
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    public R update(@RequestBody SetmealDto dto){
        log.info(dto.toString());
        setmealService.updateWhitdish(dto);
        return R.success("修改成功") ;
    }

    /**
     * 查询套餐信息
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache" ,key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
    LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
    //根据菜品的id，查询菜品的信息，状态  修改日期
    lqw.eq(setmeal.getCategoryId() != null ,Setmeal::getCategoryId,setmeal.getCategoryId());
    lqw.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
    lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(lqw);
        return  R.success(setmealList);
    }

    /**
     * 停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R status(@RequestParam List<Long> ids){
            log.info("停售：{}",ids);
    List<Setmeal> setmeals = setmealService.listByIds(ids);
    for (Setmeal setmeal : setmeals) {
        setmeal.setStatus(0);
    }
    setmealService.saveOrUpdateBatch(setmeals);
    return  R.success("修改成功");
    }
    @PostMapping("/status/1")
    public R status1(@RequestParam List<Long> ids){
        log.info("起售：{}",ids);
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(1);
        }
   setmealService.saveOrUpdateBatch(setmeals);
        return R.success("修改成功");
    }
}
